package com.xiaoge.controller;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * TODO
 *
 * @author <a href="mailto:1330137071@qq.com">Zhang Xiao</a>
 * @since
 */
@ControllerAdvice
public class ZhangXiaoControllerAdvice {

	/**
	 * 全局ModelAttribute
	 */
//	@ModelAttribute
//	public void test() {
//
//	}

	/**
	 * todo @InitBinder 是在RequestMappingHandlerAdapter被容器初始化的时候注入成bean的在生成bean的时候调用了
	 * 		afterPropertiesSet, 它里面就存了解析出来的@InitBinder方法缓存起来了
	 * @param binder
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		simpleDateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(simpleDateFormat, false)); // todo true允许输入值为null, false不允许输入值为null
	}

}
